package de.le.mfw.client.main.view;

import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.HasClickHandlers;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;

import de.le.mfw.client.main.presenter.UserPresenter.UserView;
import de.le.mfw.client.main.view.widget.ForgotPasswordWidget;
import de.le.mfw.client.main.view.widget.LoginWidget;
import de.le.mfw.client.main.view.widget.RegistrationWidget;

public class UserViewImpl implements UserView {

	private HLayout main = new HLayout();
	private RegistrationWidget registrationWidget = new RegistrationWidget();
	private LoginWidget loginWidget = new LoginWidget();
	private ForgotPasswordWidget forgotPasswordWidget = new ForgotPasswordWidget();
	private IButton logoutButton = new IButton("Logout");

	public Widget asWidget() {
		main.setWidth(300);

		main.addMember(createAreaBeforeLogin());

		return main;
	}

	private Layout createAreaBeforeLogin() {
		HLayout main = new HLayout();
		main.setMembersMargin(5);

		IButton toLogiButton = new IButton("Login");
		toLogiButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				loginWidget.show();
			}
		});

		IButton toRegisterButton = new IButton("Registrieren");
		toRegisterButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				registrationWidget.show();
			}
		});

		IButton toResetPasswordButton = new IButton("Passwort vergessen");
		toResetPasswordButton.setAutoFit(true);
		toResetPasswordButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				forgotPasswordWidget.show();
			}
		});

		main.addMember(toLogiButton);
		main.addMember(toRegisterButton);
		main.addMember(toResetPasswordButton);

		return main;
	}

	private Layout createAreaAfterLogin(String nick) {
		HLayout main = new HLayout();
		main.setMembersMargin(20);

		Label nickLabel = new Label("Hallo " + nick);
		nickLabel.setAutoFit(true);
		nickLabel.setAlign(Alignment.RIGHT);
		nickLabel.setValign(VerticalAlignment.TOP);
		main.addMember(nickLabel);
		main.addMember(logoutButton);

		return main;
	}

	public HasClickHandlers getLoginButton() {
		return loginWidget.getLoginButton();
	}

	public HasClickHandlers getRegisterNewUserButton() {
		return registrationWidget.getRegistrationButton();
	}

	public HasClickHandlers getForgotPasswordButton() {
		return forgotPasswordWidget.getResetPasswordButton();
	}

	public boolean isValid() {
		if (registrationWidget.isActive())
			return registrationWidget.isValid();
		else if (loginWidget.isActive())
			return loginWidget.isValid();
		else
			return forgotPasswordWidget.isValid();
	}

	public String getNick() {
		if (registrationWidget.isActive())
			return registrationWidget.getNick();
		else if (loginWidget.isActive())
			return loginWidget.getNick();
		else
			return forgotPasswordWidget.getNick();
	}

	public String getPassword() {
		if (registrationWidget.isActive())
			return registrationWidget.getPassword();
		else
			return loginWidget.getPassword();
	}

	public String getEmail() {
		return registrationWidget.getEmail();
	}

	public void onRegistered() {
		registrationWidget.hide();
	}

	public void onSendPassword() {
		forgotPasswordWidget.hide();
	}

	public HasClickHandlers getLogoutButton() {
		return logoutButton;
	}

	public void onLoggedIn(String nick) {
		loginWidget.hide();

		main.removeMember(main.getMember(0));
		main.addMember(createAreaAfterLogin(nick));
	}

	public void onLoggedOut() {
		main.removeMember(main.getMember(0));
		main.addMember(createAreaBeforeLogin());
	}
}
